/**
 * 
 */
package co.droidforum.foodx.workers;

import java.util.List;

import android.app.ProgressDialog;

import co.droidforum.foodx.application.GenericActivity;

/**
 * @author andres.alzate
 *
 */
public abstract class BusinessWorker extends SimpleWorker {
	
	private List<ProcessExceptionHandler> listExceptionHandlers;
	
	private ProgressDialog progressDialog;
	
	private String msgProcessWindow;
	
	public BusinessWorker(String msgProcessWindow){
		listExceptionHandlers = WorkerConfig.getListProcessExceptionHandlers();
		this.msgProcessWindow = msgProcessWindow;
	}
	
	/* 
	 * (non-Javadoc)
	 */
	@Override
	protected void onException(Exception e) {
		for(ProcessExceptionHandler processExceptionHandler : listExceptionHandlers){
			if(processExceptionHandler.isHandlerFor(e.getClass())){
				processExceptionHandler.handleException(e, this);
				break;
			}
		}
	}

	public void onCancel() {
		GenericActivity activity = GenericActivity.getActualActivity();
		activity.goToHome();
	}
	
	@Override
	protected void onBeforeRun() {
		progressDialog = ProgressDialog.show(GenericActivity.getActualActivity(), "", msgProcessWindow, true);
	}
	
	@Override
	protected void onAfterRun() {
		progressDialog.dismiss();
	}
}